#include "stdint.h"
#include "stdio.h"
#include "math.h"
#include "n32g031.h"
#include "DevicesSoftTimer.h"
#include "DevicesADC.h"
#include "DevicesBlower.h"


BlowerTypeDef g_typeBlowerInfo = {0};



void vBlowerACInit(void)
{
    GPIO_InitType GPIO_InitStructure = {0};
    EXTI_InitType EXTI_InitStructure = {0};
    NVIC_InitType NVIC_InitStructure = {0};
    TIM_TimeBaseInitType TIM_TimeBaseStructure = {0};
    OCInitType TIM_OCInitStructure = {0};

    /* GPIOx clock enable */
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO,   ENABLE);



    /* PB15 可控硅控制 */
    BLOWER_DISABLE();

    /* en gpio init */
    GPIO_InitStruct(&GPIO_InitStructure);
    /* GPIOA Configuration:TIM3 Channel1, 2, 3 and 4 as alternate function push-pull */
    GPIO_InitStructure.Pin        = GPIO_PIN_15;
    GPIO_InitStructure.GPIO_Mode  = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStructure.GPIO_Pull = GPIO_NO_PULL;
    GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_HIGH;
    GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);



    /* PA2 过零触发监测 */
    /* exit gpio init */
    GPIO_InitStruct(&GPIO_InitStructure);
    GPIO_InitStructure.Pin          = GPIO_PIN_2;
    GPIO_InitStructure.GPIO_Pull    = GPIO_PULL_UP;
    GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);

    /*Configure key EXTI Line to key input Pin*/
    GPIO_ConfigEXTILine(GPIOA_PORT_SOURCE, GPIO_PIN_SOURCE2);

    /*Configure key EXTI line*/
    EXTI_InitStructure.EXTI_Line    = EXTI_LINE2;
    EXTI_InitStructure.EXTI_Mode    = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_InitPeripheral(&EXTI_InitStructure);

    /*Set key input interrupt priority*/
    NVIC_InitStructure.NVIC_IRQChannel          = EXTI2_3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority  = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd       = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    /* 设置速度 */
    vBlowerACSpeedSet(0.0f);



    /* 定时器8用来定时控制可控硅 */
    /* TIMx clock enable */
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM8, ENABLE);

    /* Time base configuration */
    TIM_TimeBaseStructure.Period    = 10000 - 1;
    TIM_TimeBaseStructure.Prescaler = (SystemCoreClock / 1000000) - 1;;
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;
    TIM_InitTimeBase(TIM8, &TIM_TimeBaseStructure);

    /* PWM1 Mode configuration: Channelx */
    TIM_OCInitStructure.OcMode      = TIM_OCMODE_PWM1;
    TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
    TIM_OCInitStructure.Pulse       = TIM_TimeBaseStructure.Period / 2;
    TIM_OCInitStructure.OcPolarity  = TIM_OC_POLARITY_HIGH;
    TIM_OCInitStructure.OutputNState = TIM_OUTPUT_NSTATE_DISABLE;
    TIM_OCInitStructure.OcNPolarity  = TIM_OCN_POLARITY_HIGH;
    TIM_OCInitStructure.OcIdleState  = TIM_OC_IDLE_STATE_SET;
    TIM_OCInitStructure.OcNIdleState = TIM_OC_IDLE_STATE_RESET;
    TIM_InitOc3(TIM8, &TIM_OCInitStructure);
    TIM_InitOc4(TIM8, &TIM_OCInitStructure);


    /* Enable the TIMx global Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel         = TIM8_BRK_UP_TRG_COM_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelCmd      = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel         = TIM8_CC_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd      = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    /* TIMx IT enable/disable */
    TIM_ConfigInt(TIM8, TIM_INT_UPDATE, ENABLE);
    TIM_ConfigInt(TIM8, TIM_INT_CC3, ENABLE);
    TIM_ConfigInt(TIM8, TIM_INT_CC4, ENABLE);

    /* TIMx disable counter */
    TIM_Enable(TIM8, ENABLE);
}

void vBlowerACSpeedSet(float fSpeed)
{
    fSpeed = (fSpeed < 0.0f) ? 0.0f : ((fSpeed > 0.99f) ? 0.99f : fSpeed);

    g_typeBlowerInfo.speedAC = fSpeed;

    /* 频率先暂时固定为50Hz */
    g_typeBlowerInfo.frequencyAC = 50.0f;

    /* 全波脉冲 */
    g_typeBlowerInfo.time = 1000000.0f / (g_typeBlowerInfo.frequencyAC * 2.0f);
    g_typeBlowerInfo.time *= (1.0f - g_typeBlowerInfo.speedAC);
}

float fBlowerACCurrentGet(void)
{
    float fVoltage = 0.0f, fCurrent = 0.0f;

    fVoltage = fADCGetValue(ADC_DMA_CHANNEL_BLOWER);

    /* 0.1ohm 采样电阻 */
    fCurrent = fVoltage * 10.0f;

    return fCurrent;
}

float fBlowerACTemperatureGet(void)
{
    /* 单位： V   ohm   摄氏度 */
    float fVoltage = 0.0f, fResistance = 0.0f, fTemperature = 0.0f;

    fVoltage = fADCGetValue(ADC_DMA_CHANNEL_TEMPERATURE) / 1000.0f;

    /* 10k ohm 分压电阻上拉 */
    fResistance = (fVoltage * 10000.0f) / (3.3f - fVoltage);

    /* http://www.thermistors.cn/news/238.html */
    /* 10KOhm @ 25°C | 在25°C下10000欧姆| 基于FENWALL CURVE 16 | B值曲线3950K */
    if(fResistance > 3600.0f)
        fTemperature = -22.69f * log(fResistance) + 234.65f;
    else if(fResistance > 700.0f)
        fTemperature = -29.81f * log(fResistance) + 293.22f;
    else
        fTemperature = -38.33f * log(fResistance) + 349.17f;

    /* 单位： ohm */
    return fTemperature;
}
